package com.xoa.service.workflow.FlowOperation.Impl;

import com.xoa.dao.workflow.FlowOperationMapper;
import com.xoa.model.workflow.FlowOperation;
import com.xoa.model.workflow.FlowOperationExample;
import com.xoa.service.workflow.FlowOperation.FlowOperationService;
import com.xoa.util.ToJson;
import com.xoa.util.common.L;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class FlowOperationServiceImpl implements FlowOperationService {
    @Resource
    private FlowOperationMapper flowOperationMapper;

    //查询所有
    public ToJson<FlowOperation> selectAllFlowOperation() {
        ToJson<FlowOperation> json = new ToJson<FlowOperation>(1, "error");
        try {

            FlowOperationExample Example = new FlowOperationExample();
            List<FlowOperation> flowOperations = flowOperationMapper.selectByExample(Example);
            if (flowOperations.size() > 0) {
                json.setMsg("ok");
                json.setObject(flowOperations);
                json.setFlag(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    //添加
    public ToJson<FlowOperation> insertFlowOperation(FlowOperation flowOperation, HttpServletRequest request) {
        ToJson<FlowOperation> json = new ToJson<FlowOperation>(1, "error");
        int count = flowOperationMapper.insert(flowOperation);
        if (count > 0) {
            json.setMsg("ok");
            json.setFlag(0);
        } else {
            json.setFlag(1);
            json.setMsg("error");
        }
        return json;
    }

    //根据id删除
    public ToJson<FlowOperation> delFlowOperation(Integer opId) {
        ToJson<FlowOperation> json = new ToJson<FlowOperation>(1, "error");
        try {
            int count = flowOperationMapper.deleteByPrimaryKey(opId);
            if (count > 0) {
                json.setMsg("ok");
                json.setFlag(0);
            }
        } catch (Exception e) {
            json.setMsg(e.getMessage());
            L.e("FlowOperation delRelationFunc:" + e);
            e.printStackTrace();
        }
        return json;
    }

    //根据id查询单个信息
    public ToJson<FlowOperation> selFlowOperationById(Integer opId) {
        ToJson<FlowOperation> json = new ToJson<FlowOperation>(1, "error");
        try {
            FlowOperation flowOperation = flowOperationMapper.selectByPrimaryKey(opId);
            json.setObject(flowOperation);
            json.setMsg("ok");
            json.setFlag(0);
        } catch (Exception e) {
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }

    //根据id更改信息
    public ToJson<FlowOperation> updateFlowOperationById(FlowOperation flowOperation, HttpServletRequest request) {
        ToJson<FlowOperation> json = new ToJson<FlowOperation>(1, "error");

        int count = flowOperationMapper.updateByPrimaryKey(flowOperation);
        if (count > 0) {
            json.setMsg("ok");
            json.setFlag(0);
        }
        return json;
    }

}
